var APP = {};

APP.alert = function( value ) {

  if( $(".alert-msg").length ) return;

  var container = $("<div>").addClass("alert-msg"), inner = $("<span>").addClass("alert-msg-content");
  $(inner).text(value);

  $(container).append( $(inner) ).appendTo( $("body") );

  setTimeout( function(){
    $(container).css({opacity: 0});
    setTimeout( function(){ $(container).remove() }, 500);
  }, 2000 );
};


APP.showLoading = function() {
  var preloaderHtml = "<div class='preloader-modal'>正在加载</div>";
  document.body.insertAdjacentHTML( "beforeend", preloaderHtml );
};

APP.hideLoading = function() {
  var preloader = document.querySelector( ".preloader-modal" );
  preloader && preloader.parentNode.removeChild( preloader );
};

APP.storage = function( opts ) {
  if ( !opts ) return;
  var item = window.localStorage.getItem( opts.item );
  if ( item ) {
    opts.have && opts.have( item );
  } else {
    opts.nohave && opts.nohave();
  };
};

APP.checkForm = function( form, callback ) {

  typeof form === "string" && ( form = $("#" + form ) );

  var canpost = true;
  var regs = {
    m: /^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$|17[0-9]{9}/
  };

  $(form).find("input, select, textarea").each(function(index, item) {

    var type = $(this).attr("type"),
        dataReg = $(this).attr("data-type"),
        value = $(this).val().trim(),
        nullmsg = $(this).attr("data-nullmsg") || "请填写完整信息",
        errormsg = $(this).attr("data-errormsg") || "填写信息有误，请检查";

    if ( type === "hidden" || dataReg === "*" ) { // 不做任何验证
      return;
    };

    if ( !value ) {
      APP.alert( nullmsg );
      canpost = false; return false;
    };

    if ( regs[dataReg] && !regs[dataReg].test(value) ) {
      APP.alert( errormsg );
      canpost = false; return false;
    };

  });

  canpost && callback && callback.call( form );
};


Date.prototype.Format = function(fmt) { 
  var o = { 
    "M+" : this.getMonth()+1,                 //月份 
    "d+" : this.getDate(),                    //日 
    "h+" : this.getHours(),                   //小时 
    "m+" : this.getMinutes(),                 //分 
    "s+" : this.getSeconds(),                 //秒 
    "q+" : Math.floor((this.getMonth()+3)/3), //季度 
    "S"  : this.getMilliseconds()             //毫秒 
  }; 
  if(/(y+)/.test(fmt)) 
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
  for(var k in o) 
    if(new RegExp("("+ k +")").test(fmt)) 
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 
  return fmt; 
};